home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / mozilla-firefox / components / nsSidebar.js < prev    next >
Text File  |  2006-05-08  |  12KB  |  316 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  * ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1999
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *   Stephen Lamm            <slamm@netscape.com>
  24.  *   Robert John Churchill   <rjc@netscape.com>
  25.  *   David Hyatt             <hyatt@mozilla.org>
  26.  *   Christopher A. Aillon   <christopher@aillon.com>
  27.  *
  28.  * Alternatively, the contents of this file may be used under the terms of
  29.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  30.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  31.  * in which case the provisions of the GPL or the LGPL are applicable instead
  32.  * of those above. If you wish to allow use of your version of this file only
  33.  * under the terms of either the GPL or the LGPL, and not to allow others to
  34.  * use your version of this file under the terms of the MPL, indicate your
  35.  * decision by deleting the provisions above and replace them with the notice
  36.  * and other provisions required by the GPL or the LGPL. If you do not delete
  37.  * the provisions above, a recipient may use your version of this file under
  38.  * the terms of any one of the MPL, the GPL or the LGPL.
  39.  *
  40.  * ***** END LICENSE BLOCK ***** */
  41.  
  42. /*
  43.  * No magic constructor behaviour, as is de rigeur for XPCOM.
  44.  * If you must perform some initialization, and it could possibly fail (even
  45.  * due to an out-of-memory condition), you should use an Init method, which
  46.  * can convey failure appropriately (thrown exception in JS,
  47.  * NS_FAILED(nsresult) return in C++).
  48.  *
  49.  * In JS, you can actually cheat, because a thrown exception will cause the
  50.  * CreateInstance call to fail in turn, but not all languages are so lucky.
  51.  * (Though ANSI C++ provides exceptions, they are verboten in Mozilla code
  52.  * for portability reasons -- and even when you're building completely
  53.  * platform-specific code, you can't throw across an XPCOM method boundary.)
  54.  */
  55.  
  56. const DEBUG = false; /* set to false to suppress debug messages */
  57.  
  58. const SIDEBAR_CONTRACTID        = "@mozilla.org/sidebar;1";
  59. const SIDEBAR_CID               = Components.ID("{22117140-9c6e-11d3-aaf1-00805f8a4905}");
  60. const NETSEARCH_CONTRACTID      = "@mozilla.org/rdf/datasource;1?name=internetsearch"
  61. const nsISupports               = Components.interfaces.nsISupports;
  62. const nsIFactory                = Components.interfaces.nsIFactory;
  63. const nsISidebar                = Components.interfaces.nsISidebar;
  64. const nsIInternetSearchService  = Components.interfaces.nsIInternetSearchService;
  65. const nsIClassInfo              = Components.interfaces.nsIClassInfo;
  66.  
  67. function nsSidebar()
  68. {
  69.     const PROMPTSERVICE_CONTRACTID = "@mozilla.org/embedcomp/prompt-service;1";
  70.     const nsIPromptService = Components.interfaces.nsIPromptService;
  71.     this.promptService =
  72.         Components.classes[PROMPTSERVICE_CONTRACTID].getService(nsIPromptService);
  73. }
  74.  
  75. nsSidebar.prototype.nc = "http://home.netscape.com/NC-rdf#";
  76.  
  77. function sidebarURLSecurityCheck(url)
  78. {
  79.     if (url.search(/(^http:|^ftp:|^https:)/) == -1)
  80.         throw "Script attempted to add sidebar panel from illegal source";
  81. }
  82.  
  83. /* decorate prototype to provide ``class'' methods and property accessors */
  84. nsSidebar.prototype.addPanel =
  85. function (aTitle, aContentURL, aCustomizeURL)
  86. {
  87.     debug("addPanel(" + aTitle + ", " + aContentURL + ", " +
  88.           aCustomizeURL + ")");
  89.    
  90.     return this.addPanelInternal(aTitle, aContentURL, aCustomizeURL, false);
  91. }
  92.  
  93. nsSidebar.prototype.addPersistentPanel = 
  94. function(aTitle, aContentURL, aCustomizeURL)
  95. {
  96.     debug("addPersistentPanel(" + aTitle + ", " + aContentURL + ", " +
  97.            aCustomizeURL + ")\n");
  98.  
  99.     return this.addPanelInternal(aTitle, aContentURL, aCustomizeURL, true);
  100. }
  101.  
  102. nsSidebar.prototype.addPanelInternal =
  103. function (aTitle, aContentURL, aCustomizeURL, aPersist)
  104. {
  105.     var WINMEDSVC = Components.classes['@mozilla.org/appshell/window-mediator;1']
  106.                               .getService(Components.interfaces.nsIWindowMediator);
  107.     var win = WINMEDSVC.getMostRecentWindow( "navigator:browser" );
  108.                                                                                 
  109.     sidebarURLSecurityCheck(aContentURL);
  110.     var dialogArgs = {
  111.       name: aTitle,
  112.       url: aContentURL,
  113.       bWebPanel: true
  114.     }
  115.     var features;
  116.     if (!/Mac/.test(win.navigator.platform))
  117.       features = "centerscreen,chrome,dialog,resizable,dependent";
  118.     else
  119.       features = "chrome,dialog,resizable,modal";
  120.     win.openDialog("chrome://browser/content/bookmarks/addBookmark2.xul", "",
  121.                    features, dialogArgs);
  122. }
  123.  
  124. /* decorate prototype to provide ``class'' methods and property accessors */
  125. nsSidebar.prototype.addSearchEngine =
  126. function (engineURL, iconURL, suggestedTitle, suggestedCategory)
  127. {
  128.     debug("addSearchEngine(" + engineURL + ", " + iconURL + ", " +
  129.           suggestedCategory + ", " + suggestedTitle + ")");
  130.  
  131.     try
  132.     {
  133.         // make sure using HTTP (for both engine as well as icon URLs)
  134.  
  135.         if (engineURL.search(/^http:\/\//i) == -1)
  136.         {
  137.             debug ("must use HTTP to fetch search engine file");
  138.             throw Components.results.NS_ERROR_INVALID_ARG;
  139.         }
  140.  
  141.         if (iconURL.search(/^http:\/\//i) == -1)
  142.         {
  143.             debug ("must use HTTP to fetch search icon file");
  144.             throw Components.results.NS_ERROR_INVALID_ARG;
  145.         }
  146.  
  147.         // make sure engineURL refers to a .src file
  148.         if (engineURL.search(/\.src$/i) == -1)
  149.         {
  150.             debug ("engineURL doesn't reference a .src file");
  151.             throw Components.results.NS_ERROR_INVALID_ARG;
  152.         }
  153.  
  154.         // make sure iconURL refers to a .gif/.jpg/.jpeg/.png file
  155.         if (iconURL.search(/\.(gif|jpg|jpeg|png)$/i) == -1)
  156.         {
  157.             debug ("iconURL doesn't reference a supported image file");
  158.             throw Components.results.NS_ERROR_INVALID_ARG;
  159.         }
  160.  
  161.     }
  162.     catch(ex)
  163.     {
  164.         this.promptService.alert(null, "Failed to add the search engine.");
  165.         throw Components.results.NS_ERROR_INVALID_ARG;
  166.     }
  167.  
  168.     var titleMessage, dialogMessage;
  169.     try {
  170.         var stringBundle = srGetStrBundle("chrome://browser/locale/sidebar/sidebar.properties");
  171.         if (stringBundle) {
  172.             titleMessage = stringBundle.GetStringFromName("addEngineConfirmTitle");
  173.             dialogMessage = stringBundle.GetStringFromName("addEngineConfirmMessage");
  174.             dialogMessage = dialogMessage.replace(/%title%/, suggestedTitle);
  175.             dialogMessage = dialogMessage.replace(/%category%/, suggestedCategory);
  176.             dialogMessage = dialogMessage.replace(/%url%/, engineURL);
  177.             dialogMessage = dialogMessage.replace(/#/g, "\n");
  178.         }
  179.     }
  180.     catch (e) {
  181.         titleMessage = "Add Search Engine";
  182.         dialogMessage = "Add the following search engine?\n\nName: " + suggestedTitle;
  183.         dialogMessage += "\nSearch Category: " + suggestedCategory;
  184.         dialogMessage += "\nSource: " + engineURL;
  185.     }
  186.           
  187.     var rv = this.promptService.confirm(null, titleMessage, dialogMessage);
  188.       
  189.     if (!rv)
  190.         return;
  191.  
  192.     var internetSearch = Components.classes[NETSEARCH_CONTRACTID].getService();
  193.     if (internetSearch)    
  194.         internetSearch = internetSearch.QueryInterface(nsIInternetSearchService);
  195.     if (internetSearch)
  196.     {
  197.         internetSearch.AddSearchEngine(engineURL, iconURL, suggestedTitle,
  198.                                        suggestedCategory);
  199.     }
  200. }
  201.  
  202. // property of nsIClassInfo
  203. nsSidebar.prototype.flags = nsIClassInfo.DOM_OBJECT;
  204.  
  205. // property of nsIClassInfo
  206. nsSidebar.prototype.classDescription = "Sidebar";
  207.  
  208. // method of nsIClassInfo
  209. nsSidebar.prototype.getInterfaces = function(count) {
  210.     var interfaceList = [nsISidebar, nsIClassInfo];
  211.     count.value = interfaceList.length;
  212.     return interfaceList;
  213. }
  214.  
  215. // method of nsIClassInfo
  216. nsSidebar.prototype.getHelperForLanguage = function(count) {return null;}
  217.  
  218. nsSidebar.prototype.QueryInterface =
  219. function (iid) {
  220.     if (!iid.equals(nsISidebar) && 
  221.         !iid.equals(nsIClassInfo) &&
  222.         !iid.equals(nsISupports))
  223.         throw Components.results.NS_ERROR_NO_INTERFACE;
  224.     return this;
  225. }
  226.  
  227. var sidebarModule = new Object();
  228.  
  229. sidebarModule.registerSelf =
  230. function (compMgr, fileSpec, location, type)
  231. {
  232.     debug("registering (all right -- a JavaScript module!)");
  233.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  234.  
  235.     compMgr.registerFactoryLocation(SIDEBAR_CID, 
  236.                                     "Sidebar JS Component",
  237.                                     SIDEBAR_CONTRACTID, 
  238.                                     fileSpec, 
  239.                                     location,
  240.                                     type);
  241.     const CATMAN_CONTRACTID = "@mozilla.org/categorymanager;1";
  242.     const nsICategoryManager = Components.interfaces.nsICategoryManager;
  243.     var catman = Components.classes[CATMAN_CONTRACTID].
  244.                             getService(nsICategoryManager);
  245.                             
  246.     const JAVASCRIPT_GLOBAL_PROPERTY_CATEGORY = "JavaScript global property";
  247.     catman.addCategoryEntry(JAVASCRIPT_GLOBAL_PROPERTY_CATEGORY,
  248.                             "sidebar",
  249.                             SIDEBAR_CONTRACTID,
  250.                             true,
  251.                             true);
  252. }
  253.  
  254. sidebarModule.getClassObject =
  255. function (compMgr, cid, iid) {
  256.     if (!cid.equals(SIDEBAR_CID))
  257.         throw Components.results.NS_ERROR_NO_INTERFACE;
  258.     
  259.     if (!iid.equals(Components.interfaces.nsIFactory))
  260.         throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  261.     
  262.     return sidebarFactory;
  263. }
  264.  
  265. sidebarModule.canUnload =
  266. function(compMgr)
  267. {
  268.     debug("Unloading component.");
  269.     return true;
  270. }
  271.     
  272. /* factory object */
  273. var sidebarFactory = new Object();
  274.  
  275. sidebarFactory.createInstance =
  276. function (outer, iid) {
  277.     debug("CI: " + iid);
  278.     if (outer != null)
  279.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  280.  
  281.     return (new nsSidebar()).QueryInterface(iid);
  282. }
  283.  
  284. /* entrypoint */
  285. function NSGetModule(compMgr, fileSpec) {
  286.     return sidebarModule;
  287. }
  288.  
  289. /* static functions */
  290. if (DEBUG)
  291.     debug = function (s) { dump("-*- sidebar component: " + s + "\n"); }
  292. else
  293.     debug = function (s) {}
  294.  
  295. var strBundleService = null;
  296. function srGetStrBundle(path)
  297. {
  298.    var strBundle = null;
  299.    if (!strBundleService) {
  300.        try {
  301.           strBundleService =
  302.           Components.classes["@mozilla.org/intl/stringbundle;1"].getService(); 
  303.           strBundleService = 
  304.           strBundleService.QueryInterface(Components.interfaces.nsIStringBundleService);
  305.        } catch (ex) {
  306.           dump("\n--** strBundleService failed: " + ex + "\n");
  307.           return null;
  308.       }
  309.    }
  310.    strBundle = strBundleService.createBundle(path); 
  311.    if (!strBundle) {
  312.        dump("\n--** strBundle createInstance failed **--\n");
  313.    }
  314.    return strBundle;
  315. }
  316.